<?php

namespace app\api\controller;
use think\App;
use think\Controller;
use think\Db;

class Common extends Controller
{


    public function __construct()
    {
        parent::__construct();
    }
    protected function checkSign($is_login=true){
        $data = input('post.');
        if(!in_array(get_client_ip(),config('public.no_ip')) ) {
            $sign = $_SERVER['HTTP_SIGN']??'';
            $randStr = $_SERVER['HTTP_RANDSTR']??'';
            if (!$randStr) {
                return clientReturnJson(1004, '请求信息缺失');
            }
            if (!$sign) {
                return clientReturnJson(1004, '请求信息缺失!');
            }
            $sign_str = json_encode($data, JSON_UNESCAPED_UNICODE);
            $sign_str = str_replace("\\/", "/", $sign_str);
            if (!$data) {
                $sign_str = '{}';
            }
            $d = config('public.appid') . $randStr . $sign_str . config('public.key');
            if ($sign != sha1($d)) {
                return clientReturnJson(1001, '签名错误');
            }
        }
        if(!$is_login){
            return $data;
        }
        $token = isset($data['token'])?$data['token']:"";
        if(!$token){
            return  clientReturnJson(10008,'登录信息缺失');
        }
        $userInfo =redisHashGet('auth',$token);
        if(!$userInfo){
            return clientReturnJson(6666,'登录过期，请重新登录！');
        }
        if($userInfo['is_lock'] ==1){
            return clientReturnJson(1002,'当前账号已锁定！请联系客服');
        }
        return array_merge($data,$userInfo);
    }

    /**
     * @param $user_id
     * @param int $prevent 锁定 时间
     */
    protected function preventSubmit($user_id,$prevent=5){
        $user_id = md5($user_id);
        $now = time();
        $last_time= cache($user_id);
        if(!$last_time){
            cache($user_id,['overdue'=>$now+$prevent]);
        }else{
            if($last_time['overdue']>$now){
                echo clientReturnJson(666,'您操作的太频繁了,请5秒后再试!');exit;
            }else{
                cache($user_id,['overdue'=>$now+$prevent]);
            }
        }
    }
}